#include "supervision_queue.h"

/* initializes a supervision_queue */
void supervision_queue_initialize(supervision_queue* s_queue){
  int i;
  s_queue->count = 0;
  for(i=0; i < MAX_SUPERVISION; i++)
    s_queue->pcb[i] = NULL;
}							   

/* adds pcb in s_queue. Returns 1 in case of success, 0 otherwise */
int32_t supervision_queue_put(supervision_queue* s_queue, PCB* pcb){
  int i;
  if(s_queue->count >= MAX_SUPERVISION)
    return 0; //the queue is full
  for(i=0;i<MAX_SUPERVISION; i++)
    if(s_queue->pcb[i] == NULL){
      //this place is empty
      s_queue->pcb[i] = pcb;
      s_queue->count ++;
      return 1;
    }
  return 0; //should not happen
}

/* removes pcb from s_queue. Returns 1 in case of success, 0 otherwise */
int32_t supervision_queue_remove(supervision_queue* s_queue, PCB* pcb){
  int i;
  if(s_queue->count <= 0){
    return 0; //the queue is empty
  }
  for(i=0;i<MAX_SUPERVISION ;i++){
    if(s_queue->pcb[i] != NULL  && pcb_get_pid(s_queue->pcb[i]) == pcb_get_pid(pcb))
      break;
  }
  if(i == MAX_SUPERVISION)
    //target not found
    return 0;
  //i is the index of the pcb to remove
  s_queue->pcb[i] = NULL;
  s_queue->count --;
  return 1;
}
